package thread.knn.distanceAlgorithm;

import thread.knn.bean.Sample;

/**
     欧式距离计算算法
 */
public class EuclideanDistanceCalculator implements DistanceCalculator{
    //使用父类引用指向子类对象
    @Override
    public double calculate(Sample s1, Sample s2) {
        double[] data1 = s1.getExample();//样本1的特征值
        double[] data2 = s2.getExample();//样本2的特征值
        if (data1.length!= data2.length)
            throw new IllegalArgumentException("样本特征值长度不一致");
        double sum = 0;
        for (int i=0;i<data1.length;i++){
            sum += Math.pow(data1[i]-data2[i],2);
        }
        return Math.sqrt(sum);
    }
}
